﻿using System;
using System.IO;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using NetFtp.Core.Ftp;

public partial class InputBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USER_LOGIN"] == null)
            Response.Redirect("Default.aspx");

        if (Page.IsPostBack == false)
        {
            mode.Value = Request.QueryString["mode"];
            value.Value = Request.QueryString["value"];

            switch (mode.Value)
            {
                case "AddFile":
                    lblTitre.Text = GetGlobalResourceObject("General", "TitreAddFile").ToString();
                    lblTexte.Text = GetGlobalResourceObject("General", "TexteAddFile").ToString();
                    break;
                case "RenameFile":
                    lblTitre.Text = GetGlobalResourceObject("General", "TitreRenameFile").ToString();
                    lblTexte.Text = GetGlobalResourceObject("General", "TexteRenameFile").ToString();
                    FileInfo file = new FileInfo(value.Value);
                    txtValue.Text = file.Name;
                    break;
                case "AddFolder":
                    lblTitre.Text = GetGlobalResourceObject("General", "TitreMakeDirectory").ToString();
                    lblTexte.Text = GetGlobalResourceObject("General", "TexteMakeDirectory").ToString();
                    break;
            }
        }
    }

    protected void cmdOk_Click(object sender, EventArgs e)
    {
        try
        {
            FtpSession ftpSession = new FtpSession(Session["HOST_FTP"].ToString(), int.Parse(Session["PORT_FTP"].ToString()), Session["LOGIN_FTP"].ToString(), Session["MDP_FTP"].ToString(), bool.Parse(Session["PASSIVE_FTP"].ToString()));

            switch (mode.Value)
            {
                case "AddFile":
                    FileInfo file = new FileInfo(Path.Combine(Session["FOLDER_SESSION"].ToString(), txtValue.Text));
                    FileStream newfile = file.Create();
                    newfile.Close();
                    newfile.Dispose();
                    ftpSession.Upload(file.FullName, Path.Combine(value.Value, file.Name));
                    file.Delete();
                    break;
                case "RenameFile":
                    ftpSession.RenameFile(value.Value, txtValue.Text);
                    break;
                case "AddFolder":
                    ftpSession.MakeDirectory(Path.Combine(value.Value, txtValue.Text));
                    break;
            }

            ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>window.close();</script>");
        }
        catch (Exception ex)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>alert(\"Error : " + ex.Message.Replace("'", "\'") + "\");window.close();</script>");
        }
    }

    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "js1", "<script>window.close();</script>");
    }
}
